package com.yoyo.system.service.impl;

import com.yoyo.common.core.domain.Ztree;
import com.yoyo.common.core.domain.entity.SysMenu;
import com.yoyo.common.core.domain.entity.SysRole;
import com.yoyo.common.core.domain.entity.SysUser;
import com.yoyo.common.utils.StringUtils;
import com.yoyo.system.mapper.SysMenuMapper;
import com.yoyo.system.service.ISysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author zyl
 * @Date 2021/8/8 -15:38
 */
@Service
public class SysMenuServiceImpl implements ISysMenuService {

    @Autowired
    private SysMenuMapper menuMapper;

    @Override
    public List<SysMenu> selectMenusByUser(SysUser user) {
        List<SysMenu> menus;
        if (user.isAdmin()) {
            menus = menuMapper.selectMenuNormalAll();
        } else {
            menus = menuMapper.selectMenusByUserId(user.getUserId());
        }
        return getChildPerms(menus, 0);
    }

    private List<SysMenu> getChildPerms(List<SysMenu> menus, int parentId) {
        List<SysMenu> result = new LinkedList<>();
        Map<Long, SysMenu> menuMap = menus.stream()
                .collect(Collectors.toMap(SysMenu::getMenuId, Function.identity()));
        menuMap.forEach((k, v) -> {
            SysMenu parentMenu = menuMap.get(v.getParentId());
            if (StringUtils.isNotNull(parentMenu)) {
                parentMenu.getChildren().add(v);
            } else {
                result.add(v);
            }
        });

        return result;
    }

    @Override
    public List<SysMenu> selectMenuList(SysMenu sysMenu, Long userId) {
        return null;
    }

    @Override
    public List<SysMenu> selectMenuAll(Long userId) {
        return null;
    }

    @Override
    public Set<SysMenu> selectPermsByUserId(Long userId) {
        return null;
    }

    @Override
    public List<Ztree> roleMenuTreeData(SysRole role, Long userId) {
        return null;
    }

    @Override
    public List<Ztree> menuTreeData(Long userId) {
        return null;
    }

    @Override
    public Map<String, String> selectPermsAll(Long userId) {
        return null;
    }

    @Override
    public int deleteMenuById(Long menuId) {
        return 0;
    }

    @Override
    public SysMenu selectMenuById(Long menuId) {
        return null;
    }

    @Override
    public int selectCountMenuByParentId(Long parentId) {
        return 0;
    }

    @Override
    public int selectCountRoleMenuByMenuId(Long menuId) {
        return 0;
    }

    @Override
    public int insertMenu(SysMenu menu) {
        return 0;
    }

    @Override
    public int updateMenu(SysMenu menu) {
        return 0;
    }

    @Override
    public String checkMenuNameUnique(SysMenu menu) {
        return null;
    }
}
